<template>
    <!-- <div class="goodsInfo"> -->
  <!--  使用MUI提供的卡片视图代码段布局 -->
        <div class="goodsinfo-container">
            <!-- 小球显示和隐藏 -->
            <transition 
              @before-enter="beforeEnter"
              @enter="enter"
              @after-enter="afterEnter">
                <div class="ball" v-show="ballFlag" ref="ball"></div>
            </transition>

            <!-- 商品轮播图区域 -->
            <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                <!-- 御剑乘风来 除魔天地间 -->
                    <swiper :lunbotuList="lunbotu" :isfull="false"></swiper>
                </div>
            </div>
            </div>
            <!-- 商品购买区域 -->
            <div class="mui-card">
                <div class="mui-card-header">{{goodsInfo.title}}</div>
                <div class="mui-card-content">
                    <div class="mui-card-content-inner">
                        <p class="price">
                          市场价：<del>￥{{goodsInfo.market_price}}</del>&nbsp;&nbsp;销售价：<span class="now_price">￥{{goodsInfo.sell_price}}</span>
                        </p>
                        <p>购买数量：<numberbox @getcount="getSelectedCount" :max="goodsInfo.stock_quantity"></numberbox></p>
                        <p>
                          <mt-button type="primary" size="small">立即购买</mt-button>
                          <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>
                        </p>
                    </div>
                </div>
            </div>
            <!-- 商品参数区域 -->
            <div class="mui-card">
              <div class="mui-card-header">商品参数</div>
              <div class="mui-card-content">
                <div class="mui-card-content-inner">
                  <p>商品货号：{{goodsInfo.goods_no}}</p>
                  <p>库存情况：{{goodsInfo.stock_quantity}}</p>
                  <p>上架时间：{{goodsInfo.add_time | dataFormat}}</p>
                </div>
              </div>
              <div class="mui-card-footer">
                <mt-button type="primary" size="large" @click="goDesc(id)">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
              </div>
            </div>
        </div>
    <!-- </div> -->
</template>
<script>
import { getlunbotu, getGoodsInfo } from "@/api";
import swiper from "../subcomponents/swiper.vue";

import numberbox from "../subcomponents/goodsInfo_numberbox";
export default {
  data() {
    return {
      id: this.$route.params.id,
      lunbotu: [], //图片轮播
      goodsInfo: {}, //商品信息
      ballFlag: false, //控制小球显示和隐藏
      selectedCount: 1
    };
  },
  created() {
    /* 轮播图 */
    getlunbotu(this.id).then(res => {
      //console.log(res);
      if (res.data.status == 0) {
        // this.goodsList=res.data.message;

        res.data.message.forEach(item => {
          item.img = item.src;
        });
        this.lunbotu = res.data.message;
      }
    });

    /* 获取详细信息 */
    getGoodsInfo(this.id).then(res => {
      //console.log(res,88);
      if (res.data.status == 0) {
        this.goodsInfo = res.data.message[0];
      }
    });
  },
  methods: {
    /* 图文介绍 */
    goDesc(id) {
      //console.log(id);
      this.$router.push({ name: "goodsDesc", params: { id } });
    },
    /* 评论 */
    goComment(id) {
      //console.log(33322);
      this.$router.push({ name: "goodsComment", params: { id } });
    },

    /* 小球    添加购物车*/
    addToShopCar() {
      // 添加到购物车
      this.ballFlag = !this.ballFlag;

      /* 将id:商品的id,count:商品的购买数量,price:商品的单价,selected:false */
      var goodsInfo = {
        id: this.id,
        count: this.selectedCount,
        price: this.goodsInfo.sell_price,
        selected: true
      };

/*  调用vuex  state中的mountains来将数据加入购物车*/
      this.$store.commit("addToCar",goodsInfo);
    },

    /* 小球动画 */
    beforeEnter(el) {
      el.style.transform = "translate(0px,0px)";
    },
    enter(el, done) {
      el.offsetWidth;

      // 获取小球的 在页面中的位置
      const ballPosition = this.$refs.ball.getBoundingClientRect();
      // 获取 徽标 在页面中的位置
      const badgePosition = document
        .getElementById("badge")
        .getBoundingClientRect();

      const xDist = badgePosition.left - ballPosition.left;
      const yDist = badgePosition.top - ballPosition.top;

      el.style.transform = `translate(${xDist}px, ${yDist}px)`;
      el.style.transition = "all .5s cubic-bezier(.4,-0.3,1,.88)";

      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },

    /* 父组件获取子组件的值 */
    getSelectedCount(count) {
      this.selectedCount = count;
      //console.log(count, 88);
    }
  },

  components: {
    swiper,
    numberbox
  }
};
</script>

<style lang="less" scoped>
.goodsinfo-container {
  background-color: #eee;
  overflow: hidden;
  .now_price {
    color: red;
    font-size: 16px;
    font-weight: bold;
  }
  .mui-card-footer {
    display: block;
    button {
      margin: 15px 0;
    }
  }

  /* 小球 */
  .ball {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    z-index: 999;
    top: 390px;
    left: 146px;
  }
}
</style>

